using System;
using System.Drawing;
using System.Windows.Forms;

namespace EForms
{
	public class ToolBar : System.Windows.Forms.ToolBar
	{
		public ToolBar()
		{
			Appearance = ToolBarAppearance.Flat;
			SetStyle(ControlStyles.UserPaint, true);
			SetStyle(ControlStyles.DoubleBuffer, true);
			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
		}
		
		protected override void Dispose(bool disposing)
		{
			if(disposing) {
				base.Dispose(disposing);
			}
		}
		
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			Point mouse = PointToClient(Control.MousePosition);
			
			foreach(ToolBarButton button in Buttons)
			{
				bool hot = button.Rectangle.Contains(mouse);
				
				if(button.Style == ToolBarButtonStyle.Separator) {
					DrawSeparator(e.Graphics, button);
				} else {
					DrawButton(e.Graphics, hot, button.Rectangle, button);
				}
			}
		}
		
		void DrawButton(Graphics g, bool hot, Rectangle rect, ToolBarButton button)
		{
			rect = new Rectangle(rect.X, rect.Y, rect.Width -1, rect.Height - 1);
			Point mouse = PointToClient(Control.MousePosition);
			bool mouseInDropDown = false;
			bool pressed = (Control.MouseButtons == MouseButtons.Left && button.Rectangle.Contains(mouse));
			
			if(button.Style == ToolBarButtonStyle.DropDownButton) {
				Rectangle dropDownRect = new Rectangle(rect.X + rect.Width - 12, rect.Y, 12, rect.Height);
				
				mouseInDropDown = dropDownRect.Contains(mouse);
			}
			
			if(hot && button.Enabled) {
				g.FillRectangle(new SolidBrush(Style.BackColor), rect);
				
				if(pressed && ! mouseInDropDown) {
					g.FillRectangle(new SolidBrush(Style.BackColorHighlighted), rect.X, rect.Y, ButtonSize.Width, ButtonSize.Height);
				}
				
				g.DrawRectangle(new Pen(Color.Black), rect);
				
				if(button.Style == ToolBarButtonStyle.DropDownButton) {
					if(! mouseInDropDown && pressed) {
						g.DrawLine(new Pen(Color.Black), rect.Right - 13, rect.Top, rect.Right- 13, rect.Bottom);
					}
				}
			}
			
			DrawImage(g, button.ImageIndex, pressed, ! button.Enabled, rect);
			
			if(button.Style == ToolBarButtonStyle.DropDownButton) {
				DrawArrow(g, button);
			}
		}
		
		void DrawImage(Graphics g, int index, bool pressed, bool grayed, Rectangle rect)
		{
			if(index > -1 && ImageList != null && ImageList.Images.Count > index) {
				Image img = ImageList.Images[index];
				int width = ImageSize.Width;
				int height = ImageSize.Height;
				rect = new Rectangle((int)rect.X + 2 + (rect.Width - width) / 2,
				                     (int)rect.Y + (rect.Height - height) / 2,
				                     rect.Width,
				                     rect.Height);
				
				if(pressed) {
					rect = new Rectangle(rect.X + 1, rect.Y + 1, rect.Width, rect.Height);
				}
				
				if(grayed) {
					ControlPaint.DrawImageDisabled(g, img, rect.X, rect.Y, Color.Transparent);
				} else {
					g.DrawImageUnscaled(img, rect);
				}
			}
		}
		
		void DrawArrow(Graphics g, ToolBarButton button)
		{
			int x = button.Rectangle.X + button.Rectangle.Width - 9;
			int y = button.Rectangle.Y + button.Rectangle.Height - 12;
			
			if(button.Enabled) {
				g.FillPolygon(new SolidBrush(Color.Black), new Point[] { new Point(x, y), new Point(x + 5, y), new Point(x + 2, y + 3)});
			} else {
				g.FillPolygon(new SolidBrush(Color.FromArgb(166,166,166)), new Point[] {new Point(x, y), new Point(x + 5, y), new Point(x + 2, y + 3)});
			}
		}
		
		void DrawSeparator(Graphics g, ToolBarButton button)
		{
			g.DrawLine(new Pen(Color.FromArgb(166, 166, 166)),
			           button.Rectangle.X + 1,
			           button.Rectangle.Y + 2,
			           button.Rectangle.X + 1,
			           button.Rectangle.Bottom - 4);
		}
	}
}
